package work.parabola.keyboardswitch.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.parabola.keyboardswitch.common.ResponseCode;
import work.parabola.keyboardswitch.common.RoleEnum;
import work.parabola.keyboardswitch.common.ServerResponse;
import work.parabola.keyboardswitch.common.UserConst;
import work.parabola.keyboardswitch.pojo.UserSession;
import work.parabola.keyboardswitch.pojo.query.keyboard.KeyboardAddQuery;
import work.parabola.keyboardswitch.pojo.query.keyboard.KeyboardChooseSwitchQuery;
import work.parabola.keyboardswitch.pojo.query.keyboard.KeyboardListQuery;
import work.parabola.keyboardswitch.pojo.query.keyboard.KeyboardUpdateQuery;
import work.parabola.keyboardswitch.service.IKeyboardService;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/admin/keyboard/")
public class KeyboardController {

    @Autowired
    private IKeyboardService iKeyboardService;

    @GetMapping("getAdminPlateTypeList.do")
    public ServerResponse getAdminPlateTypeList(HttpSession session) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }

        return this.iKeyboardService.getAdminPlateTypeList();
    }

    @PostMapping("addAdminKeyboard.do")
    public ServerResponse addAdminKeyboard(HttpSession session, @RequestBody KeyboardAddQuery keyboardAddQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iKeyboardService.addAdminKeyboard(keyboardAddQuery, userSession);
    }

    @PostMapping("getAdminKeyboardInfo.do")
    public ServerResponse getAdminKeyboardInfo(HttpSession session, @RequestBody KeyboardListQuery keyboardListQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iKeyboardService.getAdminKeyboardInfo(keyboardListQuery, userSession);
    }

    @DeleteMapping("deleteAdminKeyboard.do/{id}")
    public ServerResponse deleteAdminKeyboard(HttpSession session, @PathVariable Integer id) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }

        return this.iKeyboardService.deleteAdminKeyboard(id);
    }

    @GetMapping("getAdminKeyboardDetailInfo.do/{id}")
    public ServerResponse getAdminKeyboardDetailInfo(HttpSession session, @PathVariable Integer id) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iKeyboardService.getAdminKeyboardDetailInfo(id);
    }

    @PostMapping("updateAdminKeyboard.do")
    public ServerResponse updateAdminKeyboard(HttpSession session, @RequestBody KeyboardUpdateQuery keyboardUpdateQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iKeyboardService.updateAdminKeyboard(keyboardUpdateQuery);
    }

    @GetMapping("getAdminKeyboardSwitchList.do/{id}")
    public ServerResponse getAdminKeyboardSwitchList(HttpSession session, @PathVariable Integer id) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iKeyboardService.getAdminKeyboardSwitchList(id);
    }

    @PostMapping("chooseAdminKeyboardSwitch.do")
    public ServerResponse chooseAdminKeyboardSwitch(HttpSession session,
                                                    @RequestBody KeyboardChooseSwitchQuery keyboardChooseSwitchQuery) {
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iKeyboardService.chooseAdminKeyboardSwitch(keyboardChooseSwitchQuery);
    }

    @PostMapping("applyAdminAllKeyboardSwitch.do/{id}")
    public ServerResponse applyAdminAllKeyboardSwitch(HttpSession session, @PathVariable Integer id){
        UserSession userSession = (UserSession) session.getAttribute(UserConst.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!userSession.getRoleType().equals(RoleEnum.MANAGER.getCode())) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PRIVILEGE_ERROR.getCode(),
                    ResponseCode.PRIVILEGE_ERROR.getDesc());
        }
        return this.iKeyboardService.applyAdminAllKeyboardSwitch(id);
    }

}
